<?php
/**
 * Notes:
 * User: sk
 * Date: 2020/9/23
 * Time: 10:28
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace App\Http\model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Dotenv\Exception;

class Jky extends Model
{
    /**
     * [erp传过来的商品id查找产品]
     * @Author   孙凯
     * @DateTime 2020-09-23
     */
    public function getproductModel($PlatProductID)
    {
        $PlatProductID = DB::table('shopsku')
            ->where('skuid', $PlatProductID)
            ->get();
        return $PlatProductID;
    }

    /**
     * [直接覆盖原来的库存数据]
     * @param $PlatProductID
     * @param $Quantity
     */
    public function syncstockModel($PlatProductID, $Quantity)
    {
        $updata = DB::table('shopsku')
            ->where('skuid', $PlatProductID)
            ->update(['stock' => $Quantity]);
        return $updata;
    }


    /**
     * [根据订单号查找商品信息]
     * @param $OrderID
     * @param $fields
     */
    public function searchOrderModel($OrderID, $fields)
    {

        $ProductInfo = DB::table('orders')
            ->where('Code', $OrderID)
            ->select($fields);
        $ProductInfo->get();

    }

    /**
     * [商品下载接口（Differ.JH.Business.DownloadProduct）]
     * @param $request
     * @return mixed
     */
    public function DownloadProductModel($request)
    {
        $data = DB::table('shopsku')
            ->select('sku.name',
                'shopsku.id as PlatProductID',
                'sku.num as OuterID',//货品编码（商家编码）
                'sku.imgurl as pictureurl',
                'shopsku.price',//jiage
                'shopsku.stock as num'//库存
            )
            ->join('sku', 'shopsku.skuid', '=', 'sku.id');

        if (!empty($request['ProductId'])) {
            $data = $data->where('sku.num', '=', $request['ProductId']);
        }

        if (!empty($request['ProductName'])) {
            $data = $data->where('name', 'like', '%' . $request['ProductName'] . '%');
        }

        if (!empty($request['Status'])) {
            if ($request['Status'] == 'JH_01') {
                $data = $data->where('shopsku.status', '=', 1);
            } elseif ($request['Status'] == 'JH_02') {
                $data = $data->where('shopsku.status', '=', 4);
            } else {
                $data = $data->where('shopsku.status', '<>', 5);
            }
        }
        $PageIndex = $request["PageIndex"] == 0 ? 0 : $request["PageIndex"] - 1;
        $totalcount = $data->get()->count();
        $res = $data->offset($PageIndex)->limit($request['PageSize'])->get()->toArray();


        $ressult['code'] = 10000;
        $ressult['message'] = "SUCCESS";
        $ressult['totalcount'] = $totalcount;
        $ressult['goodslist'] = $res;
        $ressult['skus'] = [];
        return $ressult;
    }



    /**
     * [购买等待的orders。status状态】
     * @param $value
     * @return int|string
     */
    public function CheckOrderStatusModel($value)
    {
        switch ($value) {
            case 0:
                return 'JH_01';//'JH_01'等待卖家付款
                break;
            case 3:
                return 'JH_02';//等待卖家发货=JH_02
                break;
            case 4:
                return 'JH_03';//等待买家确认收货=JH_03
            case 10:
                return 'JH_04';//交易成功=JH_04
                break;
            case 6:
                return 'JH_05';//交易关闭=JH_05
                break;
            case 11:
                return 'JH_01';//买家已经申请退款等待卖家同意=JH_01
                break;
            case 12:
                return 'JH_06';//退款成功
                break;
            case 13:
                return 'JH_04';//拒绝退款
                break;
            default:
                return 'JH_98';//其他
                break;
        }
    }


    /**
     * [商品的状态检查】
     * @param $value
     * @return string
     */
    public function CheckGoodsStatusModel($value)
    {
        switch ($value) {
            case 8:
                return 'JH_01';//已发货未收到货
                break;
            case 9:
                return 'JH_02';//已收货
                break;
            case 12:
                return 'JH_03';//已退货
                break;
            default:
                return 'JH_98';//其他
                break;
        }
    }


}